import 'package:get/get.dart';
import 'package:getx_scaffold/getx_scaffold.dart';
import 'package:oramemo/core/controllers/device_controller.dart';
import 'package:oramemo/core/controllers/record_controller.dart';

import 'facility_state.dart';

class FacilityLogic extends GetxController {
  final FacilityState state = FacilityState();
  final DeviceController deviceController = Get.find<DeviceController>();
  final RecordController recordController = Get.find<RecordController>();

  @override
  void onInit() async {
    await onLoad();
    super.onInit();
  }

  Future<void> onLoad() async {

    await requestDevices();
    await requestCallRecords(page: 1);
  }

  Future<void> requestDevices() async {
    await deviceController.fetchDevices();
    if (deviceController.devices.isNotEmpty) {
      state.data = deviceController.devices[0];
    }
    update();
  }

  Future<void> requestCallRecords({
    required int page,
    String? deviceId,
  }) async {
    deviceId=deviceId??state.data.id?.toString();
    await recordController.fetchCallRecords(page: page, deviceId: deviceId);
    update();
  }

  @override
  void onClose() {
    super.onClose();
  }
}
